<?php

/**
 * represents html code
 */
class fily_Html implements fily_ChildControl {
    /**
     * html code
     * @var string
     */
    protected $html;

    /**
     *
     * @param string $html
     */
    public function __construct($html = '') {
        $this->setHtml($html);
    }

    /**
     * returns the html code
     */
    public function toHtml() {
        return $this->getHtml();
    }

    /**
     * prepares the code for the html output
     */
    public function prepare() {
        // do nothing
    }

    /**
     * shortcut for toHtml()
     */
    public function __toString() {
        return $this->toHtml();
    }


    /**
     * sets the html code
     * @param string $html
     */
    public function setHtml($html) {
        $this->html  = $html;
    }

    /**
     * returns the html code
     * @return string
     */
    public function getHtml() {
        return $this->html;
    }

    /**
     * this control can't have a id
     * it returns a empty string
     * @return string
     */
    public function getId() {
        return '';
    }
  /**
     * system id
     * @var string
     */
    protected $sid;
        /**
     * system id
     * @return string
     */
    public function getSid(){
        return $this->sid;
    }

    /**
     * system id
     * @param string $sid
     */
    public function setSid($sid){
        $this->sid = $sid;
    }


    /**
     * parent from this control
     * @var fily_ControlToHtml
     */
    protected $parent;
    /**
     * returns the parent from this control
     * @return fily_ControlToHtml
     */
    public function getParent() {
        return $this->parent;
    }

    /**
     * set the parent from this control
     * @param fily_ControlToHtml $parent
     */
    public function setParent(fily_ControlToHtml $parent = null) {
        $this->parent = $parent;
    }

    /**
     * finds a control with the specific id
     * @param string $id
     * @return fily_ControlToHtml
     */
    public function getControlById($id) {
        // we don't have sub controls
        return null;
    }

    /**
     * finds a control with the specific system id
     * @param string $sid
     * @return fily_ControlToHtml
     */
    public function getControlBySid($sid) {
        return null;
    }



    public function find($expression) {
        $control = null;
        if(!fily_ExpressionHelper::isExpression($expression)) {
            //expression is an id
            $control = $this->getControlById($expression);
        }
        else {
            $expressionParts = fily_ExpressionHelper::getParts($expression) ;
            // first expression part is always empty
            if(count($expressionParts)>1) {
                switch ($expressionParts[1]) {
                    case 'next':
                        $control =  fily_ExpressionHelper::next($expressionParts, $expression, $this);
                        break;
                    case 'previous':
                        $control = fily_ExpressionHelper::previous($expressionParts, $expression, $this);
                        break;
                    case 'parent':
                        $control = fily_ExpressionHelper::parent($expressionParts, $expression, $this);
                        break;
                }
            }
            // found a control and have another expressions
            if($control!=null && !empty($expression)) {
                $control = $control->find($expression);
            }
        }
        return $control;
    }


}
?>
